<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-bell"></i> 消息列表 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="handle-box">
            <el-button type="success" icon="el-icon-plus" @click="Addmessage">新增消息</el-button>
        </div>
        <div class="container">
            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                <el-table-column prop="f_title" label="标题" align="center"></el-table-column>
                <el-table-column prop="f_content" label="内容"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="modification(scope.row)">修改</el-button>
                        <el-button type="text" icon="el-icon-delete" @click="del(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>    
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.page"
                    :page-size="query.pageNum"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!-- 新建 -->
        <el-dialog title="消息配置" :visible.sync="shopshow" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="标题">
                    <el-input placeholder="请输入标题" v-model="title"></el-input>
                </el-form-item>
                <el-form-item label="内容">
                    <el-input placeholder="请输入内容" type="textarea" :autosize="autosize" v-model="content"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="shopsaveEdit()">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 修改 -->
        <el-dialog title="修改消息" :visible.sync="show" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="标题">
                    <el-input placeholder="请输入标题" v-model="form.f_title"></el-input>
                </el-form-item>
                <el-form-item label="内容">
                    <el-input placeholder="请输入内容" type="textarea" :autosize="autosize" v-model="form.f_content"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
               
                <el-button type="primary" @click="modificationsave()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { getMessageList, createMessage,delMessage } from '../../../api/index';
export default {
    name: 'basetable',
    data() {
        return {
            autosize: {
                minRows: 5
            },
            query: {
                page: 1,
                pageNum: 15
            },
            tableData: [],
            editVisible: false,
            shopshow: false,
            pageTotal: 0,
            form: {},
            key: '',
            label: '',
            value: '',
            shopvalue: '',
            title: '',
            content: '',
            id:'',
            show:false,
        };
    },
    created() {
        this.getData();
    },
    methods: {
        del(e){
            delMessage({
                id: e.id,
            }).then((res) => {
                if (res.code == 0) {
                    this.$message.success(`删除成功`);
                }
                this.getData();
                // this.pageTotal = res.data.count || 50;
            });

        },
        modification(e){
            this.show = true;
            this.id = e.id
            this.form = e
            console.log(e, '修改');
        },
        modificationsave(){
            this.show = false;
              createMessage({
                id: this.id,
                title: this.form.f_title,
                content: this.form.f_content
            }).then((res) => {
                if (res.code == 0) {
                    this.$message.success(`编辑成功`);
                }
                this.getData();
                // this.pageTotal = res.data.count || 50;
            });
        },
        Addmessage() {
            this.shopshow = true;
           
        },
        // 列表展示数据
        getData() {
            getMessageList(this.query).then((res) => {
                console.log(res.data, '消息列表');
                this.tableData = res.data.list;
                // this.pageTotal = res.data.count || 50;
            });
        },
        //修改按钮点击
        check(e) {
            this.form = e;
            this.key = e.f_value;
            this.shopshow = true;
        },
        // 创建成功
        shopsaveEdit() {
            
            console.log(this.title,this.content, '消息');
            this.shopshow = false;
              createMessage({
                title: this.title,
                content: this.content
            }).then((res) => {
                if (res.code == 0) {
                    this.$message.success(`创建成功`);
                }
                this.getData();
                this.title ='',
                this.content =''
                // this.pageTotal = res.data.count || 50;
            });
            // changeConfig({
            //     key: this.form.f_key,
            //     value: this.form.f_value
            // }).then((res) => {
            //     if (res.code == 0) {
            //         this.$message.success(`修改成功`);
            //     }
            //     this.getData();
            // });
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'page', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.green {
    color: rgb(38, 141, 38);
}
.red {
    color: red;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
